package com.bjsxt.userscenter.springmvc.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userscenter.common.controller.BaseController;
import com.bjsxt.userscenter.common.util.ConstatFinalUtil;
import com.bjsxt.userscenter.common.util.PageInfoUtil;
import com.bjsxt.userscenter.users.pojo.AAdmins;
import com.bjsxt.userscenter.users.pojo.ARole;
import com.bjsxt.userscenter.users.service.IUsersService;

/**
 * 登陆后的首页面
 * 
 * url规划:凡是带/back的必须是登陆后才能操作
 * 
 * @author WangshSxtm
 */
@Controller
@RequestMapping("/back/admins")
public class AdminsBackController extends BaseController
{
	@Resource
	private IUsersService usersService;
	
	/*登陆后的首页面*/
	@RequestMapping("main")
	public String main()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--main---");
		return "/back/admins/main";
	}
	
	@RequestMapping("leftMenu")
	public String leftMenu()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--leftMenu---");
		return "/back/admins/leftMenu";
	}
	
	/*管理员模板开始*/
	/**
	 * 管理员列表
	 * @param st
	 * @param ed
	 * @param status
	 * @param keyword
	 * @param currentPage
	 * @param pageSize
	 * @param model
	 * @return
	 */
	
	@RequestMapping("adminsList")
	public String adminsList(String st,String ed,String status,String keyword,
			String currentPage,String pageSize,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--adminsList---");
		/*
		 * 将字符串变成时间
		 */
		if (st == null)
		{
			st = "";
		}
		if (ed == null)
		{
			ed = "";
		}
		if (status == null)
		{
			status = "";
		}

		/* 搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", keyword);
		if (!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}

		Date stDate = null;
		Date edDate = null;
		if (!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			/*
			 * 将字符串转换成日期
			 * 
			 * 完善了DateFormatUtil
			 */
			stDate = this.dateFormatUtil.strDateTime(st);
			edDate = this.dateFormatUtil.strDateTime(ed);
			;
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);

		PageInfoUtil pageInfoUtil = proccedPageInfo(currentPage, pageSize);

		/* 查询结果 */
		List<AAdmins> adminsList = this.usersService.findCondListAdminsService(pageInfoUtil, condMap);
		/* 将结果放到四大作用域中 */
		model.addAttribute("adminsList", adminsList);
		model.addAttribute("pageInfoUtil", pageInfoUtil);

		/* 将查询条件放到request中 */
		model.addAttribute("keyword", keyword);
		model.addAttribute("status", status);
		model.addAttribute("st", st);
		model.addAttribute("ed", ed);
		
		return "/back/admins/adminsList";
	}
	
	/**
	 * 
	 * 管理员添加页面
	 * */
	@RequestMapping("/adminsInsert")
	public String adminsInsert(Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("----adminsInsert-----");
		/*查询角色*/
		Map<String,Object> condMap = new HashMap<String,Object>();
		/*查询所有启用的角色*/
		condMap.put("status", "1");
		List<ARole> roleList = usersService.findCondListRoleService(null, condMap);
		model.addAttribute("roleList",roleList);
		return "/back/admins/adminsInsert";
	}
	
	/**
	 * 管理员添加页面提交操作
	 * 返回结果:为json
	 * {
	 * 	"statusCode":"200", 
	 * 	"message":"成功", 
	 * 	"navTabId":"", 
	 * 	"rel":"", 
	 * 	"callbackType":"", 
	 * 	"forwardUrl":"", 
	 * 	"confirmMsg":"" 
	 *	}
	 *  ResponseBody返回字符串原样输出给浏览器
	 * @return
	 */
	@RequestMapping(value="/adminsInsertSubmit",produces="text/html;charset=utf-8")
	@ResponseBody
	public String adminsInsertSubmit(HttpServletRequest request, AAdmins admins)
	{
		ConstatFinalUtil.SYS_LOGGER.info("----adminsInsertSubmit-----");
		/*用户不能赋值的，后台手动赋值*/
		/*密码加密*/
		admins.setPassword(this.encryptUtil.encodeStr(admins.getPassword()));
		/*时间*/
		admins.setCreateTime(new Date());
		admins.setUpdateTime(new Date());
		admins.setLastLoginTime(new Date());
		
		JSONObject resDbJson = this.usersService.saveOneAdminsService(admins);
		/*结果*/
		this.info = resDbJson.get("info")+"";
		
		JSONObject resultJSON = printJuiJson(request, info);
		
		return resultJSON.toJSONString();
	}
	
	/**
	 * 管理员修改
	 */
	@RequestMapping("/adminsUpdate")
	public String adminsUpdate(String aid,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("----adminsUpdate-----");
		/*查询用户角色*/
		Map<String,Object> condMap = new HashMap<String,Object>();
		condMap.put("id", aid);
		/*根据id查询管理员*/
		AAdmins admins = this.usersService.findOneAdminsService(condMap);
		model.addAttribute("admins", admins);
		
		condMap.clear();
		/*查询所有启用的角色*/
		condMap.put("status", "1");
		List<ARole> roleList = usersService.findCondListRoleService(null, condMap);
		model.addAttribute("roleList",roleList);
		return "/back/admins/adminsUpdate";
	}
	
	/**
	 * 管理员修改提交操作
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/adminsUpdateSubmit",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String adminsUpdateSubmit(HttpServletRequest request,AAdmins admins)
	{
		/*查询用户角色*/
		Map<String,Object> condMap = new HashMap<String,Object>();
		/*根据id查询用户*/
		condMap.put("id", admins.getId());
		AAdmins adminsRes = this.usersService.findOneAdminsService(condMap);
		
		/*更新信息*/
		adminsRes.setEmail(admins.getEmail());
		adminsRes.setName(admins.getName());
		adminsRes.setSex(admins.getSex());
		adminsRes.setPhone(admins.getPhone());
		adminsRes.setQq(admins.getQq());
		adminsRes.setStatus(admins.getStatus());
		adminsRes.setUpdateTime(new Date());
		
		JSONObject resJSON = this.usersService.updateOneAdminsService(adminsRes);
		this.info = resJSON.get("info")+"";
		
		JSONObject jsonObject = printJuiJson(request, info);
		return jsonObject.toJSONString();
	}
	
	
	/*管理员模板结束*/
	
	/*菜单模板开始*/
	/**
	 * 菜单列表的属性结构
	 * @return
	 */
	@RequestMapping("/menuTreeList")
	public String menuTreeList()
	{
		ConstatFinalUtil.SYS_LOGGER.info("menuList");
		return "/back/admins/menuTreeList";
	}
	
	
	/*菜单模板结束*/
	/**
	 * 退出登陆
	 * @return
	 * */
	@RequestMapping("loginOut")
	public String loginOut(HttpSession session)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--loginOut---");
		/* 删除session中的信息 */
		session.removeAttribute("admins");
		return "redirect:/login.htm";
	}
}
